home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Keyboard / Key.cp next >
Text File  |  1997-06-28  |  4KB  |  159 lines

  1. // Key.cp
  2.  
  3. #ifndef Key_h
  4. #include "Key.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9.  
  10. Key::Key( const EventRecord& event )
  11.   : code( KeyCode( Byte1( event.message ) ) ),
  12.      character( Byte0( event.message ) )
  13.   {
  14.     Normalize();
  15.     CheckCharacter();
  16.     if ( (event.modifiers & cmdKey) != 0 )
  17.         CheckForStop();
  18.   }
  19.  
  20. void Key::Normalize()
  21.   {
  22.     if ( character >= 0x20 )
  23.         return;
  24.     
  25.     static const uint8 firstAlternate[ 0x20 ] =
  26.       {
  27.            0,   0,   0,0x31,     0,   0,   0,   0,     0,   0,   0,   0,     0,0x2a,   0,   0,
  28.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,  0x3b,0x3c,0x3e,0x3d
  29.       };
  30.  
  31.     static const uint8 secondAlternate[ 0x20 ] =
  32.       {
  33.            0,   0,   0,0x34,     0,   0,   0,   0,     0,   0,   0,   0,     0,0x2a,   0,   0,
  34.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,  0x46,0x42,0x4d,0x48
  35.       };
  36.     
  37.     static const uint8 correct[ 0x20 ] =
  38.       {
  39.            0,   0,   0,0x4c,     0,   0,   0,   0,     0,   0,   0,   0,     0,0x24,   0,   0,
  40.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,  0x7b,0x7c,0x7e,0x7d
  41.       };
  42.  
  43.     if ( firstAlternate[ character ] == 0 )
  44.         return;
  45.     
  46.     if ( code == firstAlternate[ character ] ||  code == secondAlternate[ character ] )
  47.         code = KeyCode( correct[ character ] );
  48.   }
  49.  
  50. void Key::CheckCharacter()
  51.   {
  52.     static const uint8 checks[ 0x80 ] =
  53.       {
  54.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  55.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  56.            0,   0,   0,0x0d,     0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  57.         0x09,   0,   0,0x08,     0,0x1b,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  58.  
  59.            0,   0,   0,   0,     0,   0,   0,0x1b,     0,   0,   0,   0,  0x03,   0,   0,   0,
  60.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  61.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,   0,
  62.            0,   0,   0,   0,     0,   0,   0,   0,     0,   0,   0,0x1c,  0x1d,0x1f,0x1e,   0,
  63.       };
  64.  
  65.     if ( character >= 0x80 )
  66.         return;
  67.  
  68.     if ( checks[ code ] != 0 && checks[ code ] != character )
  69.         code = unknown;
  70.   }
  71.  
  72. bool Key::Special() const
  73.   {
  74.     static const uint8 specials[ maxuint8 + 1 ] =
  75.       {
  76.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  77.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  78.         0,0,0,0,  1,0,0,0,  0,0,0,0,  0,0,0,0,
  79.         1,0,0,1,  0,1,0,0,  0,0,0,0,  0,0,0,0,
  80.  
  81.         0,0,0,0,  0,0,0,1,  0,0,0,0,  1,0,0,0,
  82.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  83.         1,1,1,1,  1,1,0,1,  0,1,0,1,  0,1,0,1,
  84.         0,1,1,1,  1,1,1,1,  1,1,1,1,  1,1,1,1,
  85.  
  86.         1,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  87.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  88.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  89.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  90.  
  91.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  92.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  93.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0,
  94.         0,0,0,0,  0,0,0,0,  0,0,0,0,  0,0,0,0
  95.       };
  96.     
  97.     return specials[ code ];
  98.   }
  99.  
  100. uint16 Key::WithModifiers( EventModifiers modifiers, KCHRCache *kchr ) const
  101.   {
  102.     uint32 state = 0;
  103.     uint32 result = KeyTranslate( kchr,
  104.                                             code | ( modifiers & 0xff00 ),
  105.                                             &state );
  106.     
  107.     return (uint16( Byte3( result ) ) << 8) | uint16( Byte0( result ) );
  108.   }
  109.  
  110. KCHRCache *Key::GetKCHRCache()
  111.   {
  112.     return reinterpret_cast<KCHRCache *>( GetScriptManagerVariable( smKCHRCache ) );
  113.   }
  114.  
  115. void Key::CheckForStop()
  116.   {
  117.     if ( character == '.' )
  118.       {
  119.         code = stopKey;
  120.         return;
  121.       }
  122.     
  123.     KCHRCache * kchr = GetKCHRCache();
  124.     
  125.     if ( Byte0( WithModifiers( 0, kchr ) ) == '.'
  126.           || Byte0( WithModifiers( shiftKey, kchr ) ) == '.' )
  127.         code = stopKey;
  128.   }
  129.  
  130. uint16 Key::WithModifiers( EventModifiers modifiers ) const
  131.   {
  132.     return WithModifiers( modifiers, GetKCHRCache() );
  133.   }
  134.  
  135.  
  136. const uint8 Key::functionNumbers[ 0x20 ] =
  137.   {
  138.     5,6,7,3,   8,9,0,11,  0,13,0,14,  0,10,0,12,
  139.     0,15,0,0,  0,0,4,0,   2,0,1,0,    0,0,0,0
  140.   };
  141.  
  142. bool Key::IsFunctionKey() const
  143.   {
  144.     if ( code < 0x60 || code >= 0x80 )
  145.         return false;
  146.     
  147.     return functionNumbers[ code - 0x60 ] != 0;
  148.   }
  149.  
  150. uint32 Key::FunctionNumber() const
  151.   {
  152.     Assert( IsFunctionKey() );
  153.     
  154.     Assert( code >= 0x60 );
  155.     Assert( code < 0x80 );
  156.     
  157.     return functionNumbers[ code - 0x60 ];
  158.   }
  159.